package com.ss.android.auto.launch.tasks.collector.stage0

import android.content.Context
import android.content.SharedPreferences
import com.bytedance.librarian.Librarian
import com.ss.android.article.base.feature.app.constant.DimensionContant
import com.ss.android.article.base.utils.SharedPref.KevaSpAopHook
import com.ss.android.auto.homepage_api.IHomepageService
import com.ss.android.auto.init.AppCoreData
import com.ss.android.auto.launch.WorkerMainThreadTask
import com.ss.android.auto.launch.helper.RefreshModuleInitHelper
import com.ss.android.auto.launch.helper.SharedPreferenceInitHelper.preloadSettings
import com.ss.android.auto.launch.tasks.collector.AsyncThreadTaskCollector
import com.ss.android.auto.launch.tasks.old.WorkerBackGroundTaskNoHeader
import com.ss.android.auto.launch.tasks.old.WorkerBackgroundTaskB
import com.ss.android.auto.servicemanagerwrapper.AutoServiceManager
import com.ss.android.auto.thread.launch.LaunchExecutors
import com.ss.android.auto.utils.NewUserUtils
import com.ss.android.baseapp.CommandDispatcher
import com.ss.android.basicapi.ui.util.app.DimenHelper
import com.ss.android.basicapi.ui.util.app.FoldScreenUtils
import com.ss.android.common.app.AbsApplication
import com.ss.android.globalcard.utils.NewFeedSwitchUtil
import com.ss.android.globalcard.utils.NewFeedSwitchUtil.useNewHome805
import com.ss.android.ttwebview.AndroidQCookieUtil

/**
 *
 * 这里面适合执行一些独立的预加载的任务，不依赖其他需要初始化的库（Npth、Librarian 等关键组件会在此之前完成）
 *
 *  @author zhongxinyuan @ ByteDance Inc
 *  @since   2022/10/18
 */
object Stage0ToStage2AsyncTasks : AsyncThreadTaskCollector() {

    private val PRELOAD_SO1 = arrayOf(
        "metasec_ml",
        "perception_ml",
        "jato",
        "ttopenssl",
        "livestrategy",
        "sscronet"
    )

    override fun collectSubTasks() {
        addSubTask("preloadSettings") {
            preloadSettings(AbsApplication.getApplication())
        }
        addSubTask("preloadSo1") {
            preloadSo(PRELOAD_SO1)
        }
        addSubTask("DimensionContant") {
            DimensionContant.init(AbsApplication.getApplication())
            DimenHelper.getStatusBarHeight(AbsApplication.getApplication(), true)
            DimenHelper.getStatusBarHeight(AbsApplication.getApplication(), false)
            CommandDispatcher.getInstance()
            RefreshModuleInitHelper.initTypeMap()
            WorkerBackGroundTaskNoHeader.preloadDataBinding()
        }
        addSubTask("initSmartRouter") {
            WorkerMainThreadTask.initSmartRouter()
        }
    }

    private fun preloadSo(list: Array<String>) {
        val preloadSo = list.toMutableList()
        if (AppCoreData.getInstance().isLocalTest || AppCoreData.getInstance().isUpdate) {
            preloadSo.add("sliver")
        }
        for (so in preloadSo) {
            try {
                Librarian.loadLibrary(so)
            } catch (throwable: Throwable) {
                throwable.printStackTrace()
            }
        }
    }

    private fun preloadSp1() {
        try {
            getSharedPreferences("auto_new_location_sp", Context.MODE_PRIVATE)
            getSharedPreferences("multi_process_config", Context.MODE_MULTI_PROCESS)
            getSharedPreferences("tt_state_config", Context.MODE_MULTI_PROCESS)
            getSharedPreferences("token_shared_preference", Context.MODE_MULTI_PROCESS)
            getSharedPreferences("wschannel_multi_process_config", Context.MODE_MULTI_PROCESS)
            getSharedPreferences("key_cache_location", Context.MODE_PRIVATE)
            getSharedPreferences("com_ss_android_token_sp_host", Context.MODE_PRIVATE)
            getSharedPreferences("com.bytedance.sdk.account_setting", Context.MODE_PRIVATE)
            getSharedPreferences("com.ss.spipe_setting", Context.MODE_PRIVATE)
            getSharedPreferences(
                "com_bytedance_sdk_account_utils_common_request_cache_helper",
                Context.MODE_PRIVATE
            )

            getSharedPreferences("miniapp_preload_prefecence", Context.MODE_PRIVATE)
            getSharedPreferences("header_custom", Context.MODE_PRIVATE)
            getSharedPreferences("last_sp_session", Context.MODE_PRIVATE)
            getSharedPreferences("sp_tea_log_compress", Context.MODE_PRIVATE)

            getSharedPreferences("ss_app_config", Context.MODE_PRIVATE)
            getSharedPreferences("app_setting", Context.MODE_PRIVATE)
            getSharedPreferences("snssdk_openudid", Context.MODE_PRIVATE)
            getSharedPreferences("device_register_oaid_refine", Context.MODE_PRIVATE)
            getSharedPreferences("applog_stats", Context.MODE_PRIVATE)
            getSharedPreferences("upc_sdk_privacy", Context.MODE_PRIVATE)
            getSharedPreferences("upc_sdk_settings", Context.MODE_PRIVATE)
            getSharedPreferences("app_setting_non_persistent", Context.MODE_PRIVATE)
            getSharedPreferences("app_settings_ug", Context.MODE_PRIVATE)
            getSharedPreferences("cloud_upload_settings", Context.MODE_PRIVATE)
            getSharedPreferences("app_common_share_settings", Context.MODE_PRIVATE)
            getSharedPreferences("homepage_setting", Context.MODE_PRIVATE)
            getSharedPreferences("business_setting", Context.MODE_PRIVATE)
            getSharedPreferences("garage_setting", Context.MODE_PRIVATE)
            getSharedPreferences("interest_setting", Context.MODE_PRIVATE)
            getSharedPreferences("motor_3d_setting", Context.MODE_PRIVATE)
            getSharedPreferences("search_setting", Context.MODE_PRIVATE)
            getSharedPreferences("sp_search_data", Context.MODE_PRIVATE)
            getSharedPreferences("ss_location", Context.MODE_PRIVATE)
            getSharedPreferences("k_op_config_cache", Context.MODE_PRIVATE)
            getSharedPreferences("k_op_config_server", Context.MODE_PRIVATE)
            getSharedPreferences("operation_counter", Context.MODE_PRIVATE)
            getSharedPreferences("ttnet_tnc_config", Context.MODE_PRIVATE)

        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }

    private fun preloadSp2() {
        try {

            getSharedPreferences("main_app_settings", Context.MODE_PRIVATE)
            getSharedPreferences("image_opt_table", Context.MODE_PRIVATE)
            getSharedPreferences("auto_spread_sp", Context.MODE_PRIVATE)
            getSharedPreferences("byte_ad_tracker_preferences", Context.MODE_PRIVATE)
            getSharedPreferences("monitor_switch_config", Context.MODE_PRIVATE)
            getSharedPreferences("BDLocationCache", Context.MODE_PRIVATE)
            getSharedPreferences("bd_turning_settings_init", Context.MODE_PRIVATE)
            getSharedPreferences("BuglySdkInfos", Context.MODE_PRIVATE)
            getSharedPreferences("ug_install_settings_pref", Context.MODE_PRIVATE)
            getSharedPreferences("auto_im_conversation_sp", Context.MODE_PRIVATE)
            getSharedPreferences("account_sdk_settings_sp", Context.MODE_PRIVATE)
            getSharedPreferences("apm_cpu_front", Context.MODE_PRIVATE)
            getSharedPreferences("byte_sync_settings", Context.MODE_PRIVATE)
            getSharedPreferences("account_sdk_induce_recorder", Context.MODE_PRIVATE)
            getSharedPreferences("pref", Context.MODE_PRIVATE)
            getSharedPreferences("share_sdk_config.prefs", Context.MODE_PRIVATE)
            getSharedPreferences("update_settings.prefs", Context.MODE_PRIVATE)

//            getSharedPreferences("ies_patch", Context.MODE_PRIVATE)

            getSharedPreferences(AndroidQCookieUtil.TABLE_TTWEBVIEW_AUTO, Context.MODE_PRIVATE)

            //可能涉及SP操作，提前调用下
            FoldScreenUtils.isFoldScreenPhone()

            getSharedPreferences("sp_change_location", Context.MODE_PRIVATE)
            getSharedPreferences("abi_webview_mark", Context.MODE_PRIVATE)
            getSharedPreferences("sp_update_info", Context.MODE_PRIVATE)
            getSharedPreferences("sp_category_rad_dot", Context.MODE_PRIVATE)
            getSharedPreferences(
                "deep_link_settings_id",
                Context.MODE_PRIVATE
            )?.getBoolean("deeplink_forbid_check_clipboard", false)
            getSharedPreferences(
                "key_sp_content_sort_mode_key",
                Context.MODE_PRIVATE
            )?.getString("key_sp_global_sync_setting", "")
            getSharedPreferences("business_privacy_protect_sp", Context.MODE_PRIVATE)?.getString(
                "privacy_protect_data",
                ""
            )
        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }

    private fun getSharedPreferences(name: String, modePrivate: Int): SharedPreferences? {
        return KevaSpAopHook.getSharedPreferences(
            AbsApplication.getApplication(),
            name,
            modePrivate
        )
    }
}